// index.js
// 获取应用实例
const app = getApp()
const DB = wx.cloud.database().collection('user')
var seat_state=1
var stunum="",phonum="",stu_state=0,pho_state=0

Page({
  //初始化数据
  data: {
    userInfo: {},
    in_state:false,
    sub_state:true,
    clear_state:false,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName')
  },
  //初始化获取数据库函数
  onShow() {
    this.setData({
      sub_state:true
    });
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    var obj;
    let that = this;
    var i = setInterval(function () {
      obj = app.getUserId().openId;
      try{
        DB.doc(obj).get().then(res=>{
          stu_state=-1;
          that.setData({
            in_state:true,
            clear_state:true,
            sub_state:true,
            stunum_input:res.data.studentnum,
            phone_input:res.data.phonenum
          });
          wx.setStorage({
            key: 'number', //此处为名字
            data: res.data.studentnum, //此处为存入的数据
          });
          clearInterval(i);
        })}
      //要延时执行的代码
      catch{

      }
    }, 1000) //延迟时间 这里是1秒
  },
  login(){
   
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  // 事件处理函数
  submit(){
    let that = this;
    DB.add({
      data:{
        _id:app.getUserId().openId,
        studentnum:stunum,
        phonenum:phonum
      },
      success(res){
        wx.showToast({
          title: '登录成功',
          icon: 'none',
          duration: 1000,
        }) 
        wx.setStorage({
          key: 'number', //此处为名字
          data: stunum, //此处为存入的数据
        });
        wx.switchTab({
          url: '/pages/logs/logs'
        })
      },
      fail(res){
        wx.showToast({
          title: '注册失败',
          icon: 'none',
          duration: 1000,
        }) 
      }
    }
    )
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  confirm_stunum(e){
    var temp = e.detail.value
    stunum = temp
    let that = this
    if((stunum.length)<10){
      wx.showToast({
        title: '学号太短',
        icon: 'none',
        duration: 1000,
      })  
      that.setData({sub_state:true});
    }
    else{
      stu_state=1;
      if(stu_state==1 && pho_state==1){
        that.setData({sub_state:false});
      }
    }
  },
  confirm_phonenum(e){
    var temp = e.detail.value
    phonum = temp
    let that = this
    if((phonum.length)<11){
      wx.showToast({
        title: '手机号太短',
        icon: 'none',
        duration: 1000,
      })  
      that.setData({sub_state:true});
    }
    else{
      pho_state=1
      if(stu_state==1 && pho_state==1){
        that.setData({sub_state:false});
      }
    }
  },
  clear(){
    this.setData({
      stunum_input:"",
      phone_input:""
    })
  },
  sharesend(){
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage'],
      success:res=>{
        console.log(res)
      },
    })
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
